const {defineConfig} = require('@vue/cli-service')
const CompressionPlugin = require("compression-webpack-plugin")
const path = require("path");

function resolve(dir) {
    return path.join(__dirname, dir);
}

module.exports = defineConfig({
    transpileDependencies: true,
    chainWebpack: (config) => {
        config.plugin('html').tap(args => {
            args[0].title = "技术平台"
            return args
        })
        config.plugin('compressionPlugin').use(
            new CompressionPlugin({
                test: /\.(js|css|html)?$/i, // 压缩文件格式
                filename: "[path].gz", // 压缩后的文件名
                algorithm: "gzip", // 使用gzip压缩
                minRatio: 0.8, // 压缩率小于1才会压缩
                threshold: 10240, // 小于10KB就不进行压缩
            }))

        config.when(process.env.ENV !== "development", (config) => {
            config.optimization.splitChunks({
                chunks: "all",
                cacheGroups: {
                    libs: {
                        name: "chunk-libs",
                        test: /[\\/]node_modules[\\/]/,
                        priority: 10,
                        chunks: "initial", // only package third parties that are initially dependent
                        maxSize: 600000,
                        reuseExistingChunk: true
                    },
                    elementUI: {
                        name: "chunk-elementPlus", // split elementUI into a single package
                        priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                        test: /[\\/]node_modules[\\/]_?element-plus(.*)/, // in order to adapt to cnpm
                    },
                    commons: {
                        name: "chunk-commons",
                        test: resolve("src/components"), // can customize your rules
                        minChunks: 3, //  minimum common number
                        priority: 5,
                        reuseExistingChunk: true,
                    },
                    default: {
                        minChunks: 2,
                        priority: -20,
                        reuseExistingChunk: true
                    }
                },
            });
        });
    },
    devServer: {
        // 端口
        port: 8080,
        // 代理
        proxy: {
            [process.env.VUE_APP_UPLOAD_API]: {
                target: process.env.VUE_APP_PUBLIC_NETWORK + process.env.VUE_APP_UPLOAD_API,
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.VUE_APP_UPLOAD_API]: ''
                }
            },
            [process.env.VUE_APP_PREVIEW_API]: {
                target: process.env.VUE_APP_PUBLIC_NETWORK + process.env.VUE_APP_PREVIEW_API,
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.VUE_APP_PREVIEW_API]: ''
                }
            },
            [process.env.BASE_API]: {
                // target: 'http://10.2.71.3:8924',
                target: 'http://10.1.1.59:8601/platform/',
                changeOrigin: true,
                pathRewrite: {
                    ['^' + process.env.BASE_API]: ''
                }
            },

        }
    }
})
